﻿using IPR_StudyMeetingMrgSys.Enums;
using IPR_StudyMeetingMrgSys.Extension;
using IPR_StudyMeetingMrgSys.Models;
using log4net;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Reflection;
using System.Web.Http;

namespace IPR_StudyMeetingMrgSys.Controllers
{
    /// <summary>
    /// 试卷表
    /// </summary>
    public class ExamPaperMainController : ApiController
    {
        ILog log = log4net.LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

        public IHttpActionResult Get(int id)
        {
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                Model.ExamPaperMain model = new BLL.ExamPaperMain().GetModel(id);
                if (model != null)
                {
                    resultMsg.Info = StatusCodeEnum.Success.GetEnumText();
                    resultMsg.Data = model;
                    resultMsg.Count = 1;
                }
                else
                {
                    resultMsg.Info = "未获取到相应数据";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 试卷的增加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [Route("api/ExamPaperMain/Add")]
        [HttpPost]
        public IHttpActionResult Add(Model.ExamPaperMain model)
        {
            log.Info(JsonConvert.SerializeObject(model));
            model.EPAddEmpID = LoginUser.UserInfo.UserID;
            model.EPAddEmpName = LoginUser.UserInfo.UserName;
            model.EPAddDateTime = DateTime.Now;

            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                BLL.ExamPaperMain _ExamPaperMain = new BLL.ExamPaperMain();
                if (!_ExamPaperMain.Exists(model.EPName))
                {
                    if (new BLL.ExamPaperMain().Add(model) > 0)
                    {
                        resultMsg.Info = "新增成功";
                        resultMsg.Count = 1;
                    }
                    else
                    {
                        resultMsg.Info = "新增失败";
                    }
                }
                else
                {
                    resultMsg.Info = "试卷名称已经存在";
                }

            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 试卷查询
        /// </summary>
        /// <param name="swhere">查询条件</param>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ExamPaperMain/Search")]
        public IHttpActionResult Search(string swhere, int page, int limit)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                swhere = swhere ?? string.Empty;
                int startindex = (page - 1) * limit + 1;
                int endindex = page * limit;
                string newWhere = " 1=1 ";
                if (!string.IsNullOrWhiteSpace(swhere))
                {
                    Dictionary<string, string> jsonObject = JsonConvert.DeserializeObject<Dictionary<string, string>>(swhere);
                    foreach (var item in jsonObject)
                    {
                        if (item.Key == "EPName")
                        {
                            newWhere += $" AND EPName like '%{item.Value}%' ";
                        }
                        else if (item.Key == "EPIsRelease")
                        {
                            newWhere += $" AND EPIsRelease = {item.Value} ";
                        }
                    }
                }
                if (!LoginUser.IsSuperAdmin)
                {
                    newWhere += " AND DepIDs LIKE '%," + LoginUser.UserInfo.DeptID + ",%'";
                }

                DataSet ds = new BLL.ExamPaperMain().GetListByPage(newWhere, "EPID", startindex, endindex);
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Data = ds.Tables[0];
                resultMsg.Count = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                resultMsg.Info = resultMsg.Count > 0 ? StatusCodeEnum.Success.GetEnumText() : "未获取到相应数据";
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 试卷编辑
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/ExamPaperMain/Edit")]
        public IHttpActionResult Edit(Model.ExamPaperMain model)
        {
            log.Info(JsonConvert.SerializeObject(model));

            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                BLL.ExamPaperMain _ExamPaperMain = new BLL.ExamPaperMain();
                var oldModel = _ExamPaperMain.GetModel(model.EPID);
                if (!_ExamPaperMain.Exists(model.EPName) || oldModel.EPName == model.EPName)
                {
                    model.EPAddEmpID = oldModel.EPAddEmpID;
                    model.EPAddEmpName = oldModel.EPAddEmpName;
                    model.EPAddDateTime = oldModel.EPAddDateTime;
                    if (_ExamPaperMain.Update(model))
                    {
                        resultMsg.Info = "编辑成功";
                        resultMsg.Count = 1;
                    }
                    else
                    {
                        resultMsg.Info = "编辑失败";
                    }
                }
                else
                {
                    resultMsg.Info = "试卷名称已存在";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
        /// <summary>
        /// 试卷类型删除
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ExamPaperMain/DelList")]
        public IHttpActionResult DelList(string delidlist)
        {
            ResultMsg resultMsg = new ResultMsg();
            resultMsg.StatusCode = (int)StatusCodeEnum.Success;
            try
            {
                if (new IPR_StudyMeetingMrgSys.BLL.ExamPaperMain().DeleteList(delidlist))
                {
                    resultMsg.Info = "删除成功";
                    resultMsg.Count = 1;
                }
                else
                {
                    resultMsg.Info = "删除失败";
                }
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
        /// <summary>
        /// 根据试卷ID获取一行信息(包括部门名称信息)
        /// </summary>
        /// <param name="id">试卷ID</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ExamPaperMain/GetSingle")]
        public IHttpActionResult GetSingle(int id, string fields = "*")
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                DataTable dt = new BLL.ExamPaperMain().GetSingle(id, fields).Tables[0];
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Info = dt.Rows.Count > 0 ? StatusCodeEnum.Success.GetEnumText() : "未获取到相应数据";
                resultMsg.Data = dt;
                resultMsg.Count = dt.Rows.Count;
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }

        /// <summary>
        /// 根据试卷ID获取一行信息(在线考试加载试卷使用，data包括试卷信息及试卷包含试题信息)
        /// </summary>
        /// <param name="id">试卷ID</param>
        /// <returns></returns>
        //[HttpGet]
        //[Route("api/ExamPaperMain/GetOnlineExamInfo")]
        //public IHttpActionResult GetOnlineExamInfo(int id)
        //{
        //    ResultMsg resultMsg = new ResultMsg();
        //    try
        //    {
        //        DataSet ds = new BLL.ExamPaperMain().GetOnlineExamInfo(id);
        //        resultMsg.StatusCode = (int)StatusCodeEnum.Success;
        //        resultMsg.Info = ds.Tables[0].Rows.Count > 0 ? StatusCodeEnum.Success.GetEnumText() : "未获取到相应数据";
        //        resultMsg.Data = ds.Tables;
        //        resultMsg.Count = 2;
        //    }
        //    catch (Exception ex)
        //    {
        //        resultMsg.StatusCode = (int)StatusCodeEnum.Error;
        //        resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
        //        resultMsg.Data = ex.Message;
        //        log.Error(ex);
        //    }
        //    return Json(resultMsg);
        //}
        #region 手机端专用接口
        /// <summary>
        /// 试卷查询
        /// </summary>
        /// <param name="swhere">查询条件</param>
        /// <param name="page">页索引</param>
        /// <param name="limit">页大小</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/ExamPaperMain/SearchByDepID")]
        public IHttpActionResult SearchByDepID(int DepID)
        {
            ResultMsg resultMsg = new ResultMsg();
            try
            {
                string newWhere = "";
                if (DepID > 0)
                    newWhere = " EPIsRelease=1 AND DepIDs LIKE '%," + DepID + ",%'";

                DataSet ds = new BLL.ExamPaperMain().SearchByDepID(newWhere, "EPID");
                resultMsg.StatusCode = (int)StatusCodeEnum.Success;
                resultMsg.Data = ds.Tables[0];
                resultMsg.Count = Convert.ToInt32(ds.Tables[1].Rows[0][0]);
                resultMsg.Info = resultMsg.Count > 0 ? StatusCodeEnum.Success.GetEnumText() : "未获取到相应数据";
            }
            catch (Exception ex)
            {
                resultMsg.StatusCode = (int)StatusCodeEnum.Error;
                resultMsg.Info = StatusCodeEnum.Error.GetEnumText();
                resultMsg.Data = ex.Message;
                log.Error(ex);
            }
            return Json(resultMsg);
        }
        #endregion
    }
}
